package com.xiaolei.library.Utils;


import java.security.MessageDigest;

public class MD5
{
    /**
     * 获取字符串的MD码
     */
    public static String md5(String str)
    {
        String md5Str = "";
        try
        {
            MessageDigest mDigest = MessageDigest.getInstance("MD5");
            mDigest.update(str.getBytes());
            md5Str = bytesToHexString(mDigest.digest());
        } catch (Exception e)
        {
            md5Str = "";
        } finally
        {
            return md5Str;
        }
    }

    private static String bytesToHexString(byte[] bytes)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++)
        {
            String hex = Integer.toHexString(0xFF & bytes[i]);
            if (hex.length() == 1)
            {
                sb.append('0');
            }
            sb.append(hex);
        }
        return sb.toString();
    }
}
